<template>
<div class="login-container">
  <div class="inner">
    <div class="avator-box">
      <img src="../assets/logo-20240820.png" alt="">
    </div>
    <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px">
      <div class="platform-name">电商运营后台系统V1.0</div>
      <!--用户名-->
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          placeholder="请输入用户名"
          prefix-icon="iconfont icon-user"
        >
        </el-input>
      </el-form-item>
      <!--密码-->
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          placeholder="请输入密码"
          prefix-icon="iconfont icon-3702mima"
          type="password"
        ></el-input>
      </el-form-item>
      <!--按钮-->
      <el-form-item class="btn-right">
        <el-button @click="reset" >重置</el-button>
        <el-button type="primary" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
export default {
  name: 'login',
  data() {
    return {
      // 登录表单数据
      loginForm: {
        username: '',
        password: ''
      },
      loginFormRules: {
        // 验证用户名
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        // 验证用户名
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reset: function (){
      console.log('重置表单被点击', this)
      // 利用引用对象ref找到表单对象，重置表单，去除验证提示
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      // 登录前查看提交数据
      // console.log('登录前查看提交数据',this.loginForm)
      // 提交表单，通过表单引用，validate验证表单，返回布尔值
      this.$refs.loginFormRef.validate( async (valid)=>{
        if( !valid) return false
        const {data: res} = await this.$http.post('login', this.loginForm)
        if ( res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.$message.success('登录成功')
        // 存放token
        window.sessionStorage.setItem('token', res.data.token)
        // 存放登录信息
        // 编程时导航对象
        window.sessionStorage.setItem('userInfo', res.data.username)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container{
  height: 100%;
  background: #409EFF url("../assets/login_bg01.jpg");
  background-size: cover;
  .platform-name{
    font-size: 24px;
    color: #409EFF;
    text-align: center;
    padding: 10px;
    font-weight: bold;
  }
  .inner{
    box-sizing: border-box;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -150px;
    margin-left: -225px;
    background: #ffffff;
    width: 450px;
    height: 300px;
    padding: 10px;
    border-radius: 5px;
    .avator-box{
      position: relative;
      height: 60px;
      >img{
        position: absolute;
        top: -50px;
        left: 50%;
        margin-left: -50px;
        width: 100px;
        height: 100px;
        border: 2px solid #e2e2e2;
        border-radius: 50%;
        background: #eeeeee;
      }
    }
  }
  .btn-right{
    text-align: right;
  }
}
</style>
